(ns Server.server
  "The mediator between the database/engine and the GUI"
  (:gen-class)
  (:import (java.net Socket ServerSocket)
            (java.io PrintWriter BufferedReader InputStream InputStreamReader))
  (:use clojure.contrib.duck-streams
        Engine.readbinfile   
        clojure.contrib.gen-html-docs
    ))

(def *port* 8000)
;-------------------------------------------------------------------------------
(defn test-connection-server
  "prueba"
  [input output args]
    ;(print args)
    ;(print "jojo")
    ;(.println output "wiiiiii")
    (.println output args)
    (.flush output))
;-------------------------------------------------------------------------------
(defn test-connection-server-2
  "prueba"
  [input output]   
    (.println output "waka-waka")
    (.flush output))
;-------------------------------------------------------------------------------
(defn get-records-server
  "Get records from server"
  [input output]   
    ;(.println output "waka-waka")
    ;(.println output (get-records))
    (.println output (auxiliar))
    (.flush output))
;-------------------------------------------------------------------------------
(defn search-server
  "search by keyword"
  [input output text field]
   (if (.equals field ":name") (.println output (search-aux text :name)))
   (if (.equals field ":location")  (.println output (search-aux text :location)))
   (if (.equals field ":size") (.println output (search-aux text :size)))
   (if (.equals field ":smoking") (.println output (search-aux text :smoking)))
   (if (.equals field ":rate") (.println output (search-aux text :rate)))
   (if (.equals field ":date") (.println output (search-aux text :date)))
   (if (.equals field ":owner") (.println output (search-aux text :owner)))
  (.flush output))

;-------------------------------------------------------------------------------
(defn return-index-server
  "auxiliar method, return a list with the index of the register send"
  [input output nam location size smoking rate date owner]
  (.println output (return-index-aux nam location size smoking rate date owner))
  (.flush output)
    )
;-------------------------------------------------------------------------------
(defn edit-register-server
  "edit a register in the database, calls the edit-register
  in the readbinfile"
  [input output index nam location size smoking rate date owner]
  (edit-register-rbf (Integer. index) nam location size smoking rate date owner)
  (.println output "ok")
  (.flush output)
    )
;-------------------------------------------------------------------------------
(defn book-room-server
  "calls the method book"
  [input output index new-owner]
  (book-room-rbf (Integer. index) new-owner)
  (.println output "ok")
  (.flush output)
    )
;-------------------------------------------------------------------------------
(defn delete-register-server
  "calls the method delete, this delete a register in the database"
  [input output index]
  (delete-register-rbf (Integer. index))
  (.println output "ok")
  (.flush output)
    )
;-------------------------------------------------------------------------------
(defn create-register-server
  "create a new register in the database"
  [input output nam location size smoking rate date owner]
  (create-register-rbf nam location size smoking rate date owner)
  (.println output "ok")
  (.flush output)
    )
;-------------------------------------------------------------------------------
(defn receive
  "Check for the command send and call a function to interect with the 
  readbinfile and the database"
  [_ socket]
  (with-open [input (BufferedReader. (InputStreamReader. (.getInputStream socket)))
	            output (PrintWriter. (.getOutputStream socket))]
    (let [
          command-and-args (.readLine input)
          arreglo (.split (print-str command-and-args) "_")
          longitud (count arreglo)
          cmd (nth arreglo 0)
       ]
       (.flush output)         
         (print command-and-args)
         (if (.equals cmd "hola")
           (test-connection-server input output (nth arreglo 1))
           )
         (if (.equals cmd "waka")
           (test-connection-server-2 input output)
           )
         (if (.equals cmd "get-records")
           (get-records-server input output)
           )
         (if (.equals cmd "return-index")
           (return-index-server input output
             (nth arreglo 1)
             (nth arreglo 2)
             (nth arreglo 3)
             (nth arreglo 4)
             (nth arreglo 5)
             (nth arreglo 6)
             (if (= 7 longitud)
               ""
               (nth arreglo 7))
             )
           )
         (if (.equals cmd "edit-register-rbf")
           (edit-register-server input output
             (nth arreglo 1)
             (nth arreglo 2)
             (nth arreglo 3)
             (nth arreglo 4)
             (nth arreglo 5)
             (nth arreglo 6)
             (nth arreglo 7)
             (if (= 8 longitud)
               ""
               (nth arreglo 8))
               )
             )
         (if (.equals cmd "book-room-rbf")
           (book-room-server input output
             (nth arreglo 1)
             (if (= 2 longitud)
               ""
               (nth arreglo 2))
               )
             )
         (if (.equals cmd "delete-register-rbf")
           (delete-register-server input output
             (nth arreglo 1)
               )
             )
         (if (.equals cmd "create-register-rbf")
           (create-register-server input output
             (nth arreglo 1)
             (nth arreglo 2)
             (nth arreglo 3)
             (nth arreglo 4)
             (nth arreglo 5)
             (nth arreglo 6)
             (if (= 7 longitud)
               ""
               (nth arreglo 7))
             )
           )
         (if (.equals cmd "search")
           (search-server input output
            (if (= 2 longitud)
               ""
               (nth arreglo 1))
             (nth arreglo 2))
           )
           )
	     ))
;-------------------------------------------------------------------------------
(defn init
  "Init the server
  Listen the port 8000"
  [port]
  (let [
    server (ServerSocket. port)
	  agent  (agent nil)
	  ]
    (loop []
      (println "Waiting for connections")
      (let [socket (.accept server)]
        (send-off agent receive socket))
    (recur))))
;-------------------------------------------------------------------------------
(defn -main
  []
(init *port*))

(generate-documentation-to-file "./server.html" ['Server.server])
